import router from "../router";

export default {
    state: {
        stretchBtnIconStatus: false, //控制左侧菜单栏菜单伸缩收起
        stretchBtnIconStatusWidth: 220,//左侧菜单栏展开时的宽度
        menuData: [],
        isInitRouter: false,
    },
    mutations: {
        changeStretchBtnIconStatus(state) {
            state.stretchBtnIconStatus = !state.stretchBtnIconStatus;
            state.stretchBtnIconStatusWidth = state.stretchBtnIconStatus ? 90 : 220;
        },
        initMenuData(state, data) {
            let menuData = {
                'menu': {
                    'data': data.menu,
                    'activeMenuName': data.menu[0].name,
                    'activeId': data.menu[0].id,
                },
                'siderMenu': {
                    'data': data.menu[0].son_menu,
                    'isOpen': true,//是否展开，展开不代表选中，仅仅是展开
                    'openId': data.menu[0].son_menu[0].id,//展开的ID
                    'isActive': true,//是否选中，选中不代表展开
                    'activeId': data.menu[0].son_menu[0].id,
                },
                'sonMenu': {
                    'data': [],
                    'activeId': 0,
                },
                'breadcrumb': {
                    'data': [
                        data.menu[0],
                        data.menu[0].son_menu[0],
                    ]
                },
                'tagNavList': {
                    'data': [
                        data.menu[0].son_menu[0]
                    ],
                    'width': 360,
                    'scrollMainWidth': 360,
                    'left': 0,
                    'activeId': data.menu[0].son_menu[0]['id']
                },
                'btnMenu': data.btn
            }
            state.menuData = menuData;

            this.commit('initRoute');
        },
        initRoute(state) {
            //添加动态路由
            let index = router.getRoutes().findIndex(item => item.path === '/user/label/list');
            if (index < 0 && state.menuData.menu) {
                state.menuData.menu.data.forEach(i => {//顶级菜单
                    if (i.son_menu.length > 0) {
                        i.son_menu.forEach(ii => {//二级菜单
                            if (ii.son_menu.length > 0) {
                                ii.son_menu.forEach(iii => {//三级菜单
                                    if (iii.path) {
                                        router.addRoute('main', {
                                            path: `/views${iii.path}`,
                                            name: ii.name,
                                            component: (resolve) => require([`@/views${iii.path}`], resolve),
                                        });
                                    }
                                })
                            } else {
                                if (ii.path) {
                                    router.addRoute('main', {
                                        path: `/views${ii.path}`,
                                        name: ii.name,
                                        component: (resolve) => require([`@/views${ii.path}`], resolve),
                                    });
                                }
                            }
                        })
                    }
                });

                //添加按钮级菜单路由
                state.menuData.btnMenu.forEach(i => {
                    router.addRoute('main', {
                        path: `/views${i.path}`,
                        name: i.name,
                        component: (resolve) => require([`@/views${i.path}`], resolve),
                    });
                });

                //404路由
                router.addRoute('main', {
                    path: '*',
                    name: 'notFound',
                    component: (resolve) => require([`@/views/notFound`], resolve),
                });
            }
        },
        //点击侧栏顶级、二级、三级菜单
        clickMenu(state, param) {
            switch (param.type) {
                case 1:
                    state.menuData.menu.activeId = param.menuInfo.id;
                    var tempMenu = state.menuData.menu.data;
                    var resMenu = [];
                    for (let i = 0; i < tempMenu.length; i++) {
                        if (tempMenu[i].id == param.menuInfo.id) {
                            resMenu = tempMenu[i].son_menu;
                        }
                    }

                    //赋值2级菜单数据
                    state.menuData.menu.activeMenuName = param.menuInfo.name;
                    state.menuData.siderMenu.data = resMenu;
                    state.menuData.siderMenu.openId = resMenu[0].id;
                    state.menuData.siderMenu.activeId = resMenu[0].id;
                    state.menuData.siderMenu.isOpen = true;

                    //赋值3级菜单数据
                    if (state.menuData.siderMenu.data[0].son_menu.length) {
                        state.menuData.sonMenu.data = state.menuData.siderMenu.data[0].son_menu;
                        state.menuData.sonMenu.activeId = state.menuData.siderMenu.data[0].son_menu[0].id;
                        //赋值tab菜单
                        let index = state.menuData.tagNavList.data.findIndex(item => item.id === state.menuData.siderMenu.data[0].son_menu[0].id);
                        if (index < 0) {
                            state.menuData.tagNavList.data.push(state.menuData.siderMenu.data[0].son_menu[0]);
                            state.menuData.tagNavList.activeId = state.menuData.siderMenu.data[0].son_menu[0].id;
                            state.menuData.tagNavList.width += 120;
                        }
                    } else {
                        state.menuData.sonMenu.data = [];
                        state.menuData.sonMenu.activeId = 0;

                        let indexA = state.menuData.tagNavList.data.findIndex(item => item.id === state.menuData.siderMenu.data[0].id);
                        if (indexA < 0) {
                            state.menuData.tagNavList.data.push(state.menuData.siderMenu.data[0]);
                        }
                    }

                    //赋值面包屑数据
                    state.menuData.breadcrumb.data = [
                        param.menuInfo,
                        state.menuData.siderMenu.data[0],
                    ];
                    if (state.menuData.sonMenu.data[0] !== undefined) {
                        state.menuData.breadcrumb.data[2] = state.menuData.sonMenu.data[0];
                        state.menuData.tagNavList.activeId = state.menuData.sonMenu.data[0].id;

                        //执行tab菜单位置切换
                        let indexLet = state.menuData.tagNavList.data.findIndex(item => item.id === state.menuData.sonMenu.data[0].id);
                        this.commit('setTagNavListDomLeft', indexLet);
                    } else {
                        //执行tab菜单位置切换
                        let indexLet = state.menuData.tagNavList.data.findIndex(item => item.id === state.menuData.siderMenu.activeId);
                        state.menuData.tagNavList.activeId = state.menuData.siderMenu.activeId;
                        this.commit('setTagNavListDomLeft', indexLet);
                    }
                    break;
                case 2:
                    if (param.menuInfo.son_menu.length > 0) {
                        if (state.menuData.siderMenu.openId != param.menuInfo.id && state.menuData.siderMenu.isOpen === true) {
                            state.menuData.siderMenu.openId = param.menuInfo.id;
                        } else if (state.menuData.siderMenu.openId != param.menuInfo.id && state.menuData.siderMenu.isOpen === false) {
                            state.menuData.siderMenu.openId = param.menuInfo.id;
                            state.menuData.siderMenu.isOpen = true;
                        } else if (state.menuData.siderMenu.openId == param.menuInfo.id && state.menuData.siderMenu.isOpen === true) {
                            state.menuData.siderMenu.isOpen = false;
                        } else if (state.menuData.siderMenu.openId == param.menuInfo.id && state.menuData.siderMenu.isOpen === false) {
                            state.menuData.siderMenu.isOpen = true;
                        }
                    } else {
                        state.menuData.sonMenu.activeId = 0;
                        state.menuData.siderMenu.activeId = param.menuInfo.id;
                        state.menuData.siderMenu.isActive = true;
                        //赋值tab菜单
                        let index = state.menuData.tagNavList.data.findIndex(item => item.id === param.menuInfo.id);
                        if (index < 0) {
                            state.menuData.tagNavList.data.push(param.menuInfo);
                            state.menuData.tagNavList.activeId = param.menuInfo.id;
                            state.menuData.tagNavList.width += 120;
                        } else {
                            state.menuData.tagNavList.activeId = param.menuInfo.id;
                        }
                        //更新面包屑菜单
                        let indexId = state.menuData.breadcrumb.data.findIndex(item => item.id === param.menuInfo.id);
                        if (indexId < 0) {
                            state.menuData.breadcrumb.data = [
                                state.menuData.breadcrumb.data[0],
                                param.menuInfo,
                            ]
                        }

                        //执行tab菜单位置切换
                        let indexLet = state.menuData.tagNavList.data.findIndex(item => item.id === param.menuInfo.id);
                        this.commit('setTagNavListDomLeft', indexLet);
                    }
                    break;
                case 3:
                    state.menuData.sonMenu.activeId = param.menuInfo.id;
                    for (let i = 0; i < state.menuData.menu.data.length; i++) {
                        if (state.menuData.menu.data[i].son_menu.length > 0) {
                            for (let j = 0; j < state.menuData.menu.data[i].son_menu.length; j++) {
                                if (state.menuData.menu.data[i].son_menu[j].son_menu.length > 0) {
                                    for (let h = 0; h < state.menuData.menu.data[i].son_menu[j].son_menu.length; h++) {
                                        if (state.menuData.menu.data[i].son_menu[j].son_menu[h].id == param.menuInfo.id) {
                                            state.menuData.siderMenu.activeId = state.menuData.menu.data[i].son_menu[j].id;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    //赋值tab菜单
                    var index = state.menuData.tagNavList.data.findIndex(item => item.id === param.menuInfo.id);
                    if (index < 0) {
                        state.menuData.tagNavList.data.push(param.menuInfo);
                        state.menuData.tagNavList.activeId = param.menuInfo.id;
                        state.menuData.tagNavList.width += 120;
                    } else {
                        state.menuData.tagNavList.activeId = param.menuInfo.id;
                    }

                    //渲染选中的主菜单侧边栏菜单等
                    this.commit('renderingMenu', state.menuData.tagNavList.activeId);

                    //执行tab菜单位置切换
                    this.commit('setTagNavListDomLeft', index);
                    break;
            }

            //当侧栏菜单被收叠并且系统主题设置了点击菜单展开子菜单时，点击菜单会展开子菜单
            if (state.stretchBtnIconStatus && param.clickMenuIsOpenSideMenu) {
                state.stretchBtnIconStatus = false;
                state.stretchBtnIconStatusWidth = state.stretchBtnIconStatus ? 90 : 220;
            }
        },
        //删除tab栏菜单
        delTagNavList(state, param) {
            var index = state.menuData.tagNavList.data.findIndex(item => item.id === param.id);
            if (index >= 1 && state.menuData.tagNavList.activeId == param.id) {
                state.menuData.tagNavList.activeId = state.menuData.tagNavList.data[index - 1].id;
            } else if (state.menuData.tagNavList.data[index + 1] && state.menuData.tagNavList.activeId == param.id) {
                state.menuData.tagNavList.activeId = state.menuData.tagNavList.data[index + 1].id
            }
            state.menuData.tagNavList.data.splice(index, 1);
            state.menuData.tagNavList.width = state.menuData.tagNavList.data.length * 120;

            //执行tab菜单位置切换
            let indexLet = state.menuData.tagNavList.data.findIndex(item => item.id === state.menuData.tagNavList.activeId);
            this.commit('setTagNavListDomLeft', indexLet);

            //渲染选中的主菜单侧边栏菜单等
            this.commit('renderingMenu', state.menuData.tagNavList.activeId);
        },
        //选中tab栏菜单
        selectTagNavList(state, param) {
            if (state.menuData.tagNavList.activeId !== param.menuInfo.id) {
                state.menuData.tagNavList.activeId = param.menuInfo.id
            }
            //渲染选中的主菜单侧边栏菜单等
            this.commit('renderingMenu', param.menuInfo.id);

            //当侧栏菜单被收叠并且系统主题设置了点击菜单展开子菜单时，点击菜单会展开子菜单
            if (state.stretchBtnIconStatus && param.clickMenuIsOpenSideMenu) {
                state.stretchBtnIconStatus = false;
                state.stretchBtnIconStatusWidth = state.stretchBtnIconStatus ? 90 : 220;
            }

            //清除按钮等级菜单的文本
            this.commit('removeBtnMenuText');
        },
        //初始化tab菜单栏宽度
        setTagNavListScrollMainWidth(state, width) {
            state.menuData.tagNavList.scrollMainWidth = width;
        },
        //设置tab菜单left值，达到滚动效果
        setTagNavListDomLeft(state, index) {
            if (index * 120 >= (state.menuData.tagNavList.scrollMainWidth - 360)) {
                state.menuData.tagNavList.left = -(index * 120 - state.menuData.tagNavList.scrollMainWidth + 240);
                state.menuData.tagNavList.left = state.menuData.tagNavList.left > 0 ? 0 : state.menuData.tagNavList.left;
            } else {
                state.menuData.tagNavList.left = 0;
            }
            state.menuData.tagNavList.width = state.menuData.tagNavList.data.length * 120;
        },
        //添加tab栏菜单箭头
        clickTagNavListArrow(state, type) {
            if (type === 'left') {
                if (state.menuData.tagNavList.left < 0) {
                    state.menuData.tagNavList.left += 120;
                    state.menuData.tagNavList.left = state.menuData.tagNavList.left > 0 ? 0 : state.menuData.tagNavList.left;
                }
            }
            var scrollMainWidth = state.menuData.tagNavList.scrollMainWidth;
            if (type === 'right' && state.menuData.tagNavList.width > scrollMainWidth && ((state.menuData.tagNavList.width + state.menuData.tagNavList.left) > scrollMainWidth)) {
                state.menuData.tagNavList.left -= 120;
            }
        },
        //关闭tab栏菜单
        closeTagMenu(state, type) {
            if (type === 'all') {
                state.menuData.tagNavList.data.splice(1, 9999);
                state.menuData.tagNavList.activeId = state.menuData.tagNavList.data[0].id;
                state.menuData.tagNavList.width = state.menuData.tagNavList.data.length * 120;

                //渲染选中的主菜单侧边栏菜单等
                this.commit('renderingMenu', state.menuData.tagNavList.data[0].id);

                //执行tab菜单位置切换
                let indexLet = state.menuData.tagNavList.data.findIndex(item => state.menuData.tagNavList.data[0].id);
                this.commit('setTagNavListDomLeft', indexLet);
            }
            if (type === 'other') {
                let data = [
                    state.menuData.tagNavList.data[0]
                ]
                for (let i = 0; i < state.menuData.tagNavList.data.length; i++) {
                    if (i > 0 && state.menuData.tagNavList.data[i].id == state.menuData.tagNavList.activeId) {
                        data.push(state.menuData.tagNavList.data[i]);
                    }
                }
                state.menuData.tagNavList.data = data;

                //执行tab菜单位置切换
                let indexLet = state.menuData.tagNavList.data.findIndex(item => item.id === state.menuData.tagNavList.activeId);
                this.commit('setTagNavListDomLeft', indexLet);

                //渲染选中的主菜单侧边栏菜单等
                this.commit('renderingMenu', state.menuData.tagNavList.activeId);
            }
        },
        //渲染选中的主菜单侧边栏菜单等
        renderingMenu(state, menuId) {
            for (let i = 0; i < state.menuData.menu.data.length; i++) {
                if (state.menuData.menu.data[i].son_menu.length > 0) {
                    for (let j = 0; j < state.menuData.menu.data[i].son_menu.length; j++) {
                        if (state.menuData.menu.data[i].son_menu[j].id === menuId) {
                            //顶部菜单
                            state.menuData.menu.activeId = state.menuData.menu.data[i].id;
                            state.menuData.menu.activeMenuName = state.menuData.menu.data[i].name;

                            //二级菜单
                            state.menuData.siderMenu.data = state.menuData.menu.data[i].son_menu;
                            state.menuData.siderMenu.isOpen = true;
                            state.menuData.siderMenu.openId = state.menuData.menu.data[i].son_menu[j].id;
                            state.menuData.siderMenu.isActive = true;
                            state.menuData.siderMenu.activeId = state.menuData.menu.data[i].son_menu[j].id;

                            //面包屑
                            state.menuData.breadcrumb.data = [
                                state.menuData.menu.data[i],
                                state.menuData.menu.data[i].son_menu[j]
                            ];
                        }
                        if (state.menuData.menu.data[i].son_menu[j].son_menu.length > 0) {
                            for (let h = 0; h < state.menuData.menu.data[i].son_menu[j].son_menu.length; h++) {
                                if (state.menuData.menu.data[i].son_menu[j].son_menu[h].id == menuId) {
                                    state.menuData.siderMenu.activeId = state.menuData.menu.data[i].son_menu[j].id;

                                    //顶部菜单
                                    state.menuData.menu.activeId = state.menuData.menu.data[i].id;
                                    state.menuData.menu.activeMenuName = state.menuData.menu.data[i].name;

                                    //二级菜单
                                    state.menuData.siderMenu.data = state.menuData.menu.data[i].son_menu;
                                    state.menuData.siderMenu.isOpen = true;
                                    state.menuData.siderMenu.openId = state.menuData.menu.data[i].son_menu[j].id;
                                    state.menuData.siderMenu.isActive = true;
                                    state.menuData.siderMenu.activeId = state.menuData.menu.data[i].son_menu[j].id;

                                    //三级菜单
                                    state.menuData.sonMenu.data = state.menuData.menu.data[i].son_menu[j].son_menu;
                                    state.menuData.sonMenu.activeId = state.menuData.menu.data[i].son_menu[j].son_menu[h].id;

                                    //面包屑
                                    state.menuData.breadcrumb.data = [
                                        state.menuData.menu.data[i],
                                        state.menuData.menu.data[i].son_menu[j],
                                        state.menuData.menu.data[i].son_menu[j].son_menu[h]
                                    ];
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}